<?php

namespace App\Console\Commands\Shop;

use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopAttributeCategoryModel;
use App\Kuafu\Shop\Model\ShopAttributeMappingModel;
use App\Kuafu\Shop\Model\ShopAttributeModel;
use App\Remote\Tiger\OpenShop\OpenShopRemote;
use Illuminate\Console\Command;

class SyncShopAttributeDay extends Command
{
    protected $signature = 'shop:syncShopAttributeDay';

    protected $description = '每天同步门店属性-门店状态';

    public function handle()
    {

        // 门店属性值列表
        $shopAttributeList = ShopAttributeModel::query()
                                               ->whereIn('categoryId',
                                                   [ShopAttributeCategoryModel::CATEGORY_ID_12])// 门店状态
                                               ->get()
                                               ->map(function ($item) {
                                                   return [
                                                       'id'   => $item->id,
                                                       'name' => $item->name,
                                                   ];
                                               })->keyBy('name')->toArray();


        $createData = [];
        Shop::query()
            ->select(['id', 'name'])
            ->whereIn('status', [CodeEnum::SHOP_STATUS_NORMAL, CodeEnum::SHOP_STATUS_PREPARE])//启用+筹备中
            ->chunkById(500, function ($shops) use ($shopAttributeList, &$createData) {
                $shopIds  = array_column($shops->toArray(), 'id');
                $shopList = collect(OpenShopRemote::getShopAttributeByShopIds($shopIds, 'guanyuan') ?? [])->keyBy('id')->toArray();
                foreach ($shopList as $shop) {
                    if (in_array($shop['status'], ['选址中', '筹备中', '正常营业', '暂停营业', '闭店'])) {
                        $newId = $shopAttributeList[$shop['status']]['id'] ?? 0;// 当前状态对应属性id

                        // 查询当前门店 门店状态对应属性
                        $res = self::getShopAttributeData($shop['id'], array_column($shopAttributeList, 'id'));
                        if ($res) {
                            if ($res->attributeId != $newId) {
                                ShopAttributeMappingModel::updateData($res->id, ['attributeId' => $newId]);
                            }
                        } else {
                            $createData[] = [
                                'shopId'      => $shop['id'],
                                'attributeId' => $newId,
                                'value'       => '',
                                'createdAt'   => time(),
                            ];
                        }
                    }

                }
            });

        if ($createData) {
            $chunk = array_chunk($createData, 500);
            foreach ($chunk as $value) {
                ShopAttributeMappingModel::query()->insert($value);
            }
        }
    }

    public function getShopAttributeData($shopId, $attributeId)
    {
        return ShopAttributeMappingModel::query()
                                        ->join('shop_attribute', 'shop_attribute.id', '=',
                                            'shop_attribute_mapping.attributeId')
                                        ->where('shop_attribute_mapping.shopId', $shopId)
                                        ->whereIn('shop_attribute.id', $attributeId)
                                        ->first();
    }
}
